<?php

class CDatabase {

	var $_connection;
	var $_dbserver;
	var $_dbuser;
	var $_dbpass;
	var $_dbname;

	var $results;
	var $error;
	var $insertID;
	var $rowsnumber;
	var $access;

	function CDatabase() { 
		// initialize database variables
		$this->_dbserver = DB_SERVER;
		$this->_dbuser = DB_SERVER_USERNAME;
		$this->_dbpass = DB_SERVER_PASSWORD;
		$this->_dbname = DB_DATABASE;
	}
	
	function Connect() {
		// open connection 
		$this->_connection = mysql_pconnect($this->_dbserver,$this->_dbuser,$this->_dbpass) or die ("Sorry, cannot connect to the database.");
		// select database
		mysql_select_db($this->_dbname,$this->_connection) or die ("Unable to select database!");
	}

	function Query($query) {
		//excute sql query
		$this->results = mysql_query($query); // or die("Database is connected, but could not read from or write data to it.");
		//catch error
		if ($this->error = mysql_error()) return false;
		//get insert id if necessary
		if (substr($query,0,6)=="INSERT") $this->insertID = mysql_insert_id();
		//get number of rows returned or affected
		if (substr($query,0,6)=="SELECT") $this->rowsnumber = mysql_num_rows($this->results);
		else $this->rowsnumber = mysql_affected_rows();
		return true;
	}

	function getRow() {
		//retrieve sql query resultset into variable
		return $this->access = mysql_fetch_array($this->results);
	}
	
	function getResults() {
		if ($this->results) {
			mysql_data_seek($this->results, 0);
			while($row = mysql_fetch_array($this->results)) $rows[] = $row;
			mysql_data_seek($this->results, 0);
		} else $rows = false;
		return $rows;
	}

}
?>